# 角色基类 - 定义玩家和敌人的共同属性和方法
class_name Character

# 基本属性
var name: String = "Character"
var max_hp: int = 100
var current_hp: int = 100
var attack: int = 10
var defense: int = 5
var speed: int = 5
var is_player: bool = false
var is_alive: bool = true

# 角色节点引用
var node: Node2D
var sprite: Sprite2D


# 初始化角色
func _init(node_ref: Node2D, sprite_ref: Sprite2D = null):
	node = node_ref
	sprite = sprite_ref


# 受到伤害
func take_damage(amount: int) -> int:
	# 计算实际伤害（考虑防御力）
	var actual_damage = max(1, amount - defense)
	current_hp = max(0, current_hp - actual_damage)

	# 检查是否死亡
	if current_hp <= 0:
		is_alive = false
		on_death()

	return actual_damage


# 恢复生命值
func heal(amount: int) -> int:
	var old_hp = current_hp
	current_hp = min(max_hp, current_hp + amount)
	return current_hp - old_hp


# 死亡时的回调
func on_death():
	# 子类可以重写此方法以实现特定的死亡逻辑
	if sprite:
		sprite.modulate = Color(0.5, 0.5, 0.5, 0.7)  # 变为灰色半透明


# 获取战斗顺序值（用于决定行动顺序）
func get_initiative() -> float:
	# 基础速度 + 随机因素，使战斗顺序更加动态
	return speed + randf_range(-1.0, 1.0)


# 获取角色状态信息
func get_status_info() -> Dictionary:
	return {
		"name": name,
		"max_hp": max_hp,
		"current_hp": current_hp,
		"attack": attack,
		"defense": defense,
		"speed": speed,
		"is_player": is_player,
		"is_alive": is_alive
	}
